package com.bjpowernode.crm.workbench.web.controller;

import com.bjpowernode.crm.commons.contants.Contants;
import com.bjpowernode.crm.commons.domain.ReturnObject;
import com.bjpowernode.crm.commons.utils.DateUtils;
import com.bjpowernode.crm.commons.utils.UUIDUtils;
import com.bjpowernode.crm.settings.domain.DicValue;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.settings.service.DicValueService;
import com.bjpowernode.crm.workbench.domain.Activity;
import com.bjpowernode.crm.workbench.domain.Clue;
import com.bjpowernode.crm.workbench.domain.ClueActivityRelation;
import com.bjpowernode.crm.workbench.domain.ClueRemark;
import com.bjpowernode.crm.workbench.service.ActivityService;
import com.bjpowernode.crm.workbench.service.ClueActivityRelationService;
import com.bjpowernode.crm.workbench.service.ClueRemarkService;
import com.bjpowernode.crm.workbench.service.ClueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class ClueDetailController {

    @Autowired
    private ClueRemarkService clueRemarkService;

    @Autowired
    private ActivityService activityService;

    @Autowired
    private ClueActivityRelationService clueActivityRelationService;

    @Autowired
    private DicValueService dicValueService;

    @Autowired
    private ClueService clueService;

    @RequestMapping(value = "/workbench/clue/saveCreateClueRamark.do")
    @ResponseBody
    public Object saveCreateClueRamark(String remark, String clueId, HttpSession session){
        ReturnObject returnObject = new ReturnObject();

        User user = (User)session.getAttribute(Contants.SESSION_USER);

        ClueRemark clueRemark = new ClueRemark();

        clueRemark.setId(UUIDUtils.getUUID());
        clueRemark.setCreateBy(user.getId());
        clueRemark.setCreateTime(DateUtils.formatDataTime(new Date()));
        clueRemark.setNoteContent(remark);
        clueRemark.setClueId(clueId);

        try {
            int res = clueRemarkService.saveCreateClueRemark(clueRemark);
            if(res > 0){
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
                clueRemark.setCreateBy(user.getName());
                returnObject.setRetData(clueRemark);
            }else{
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("系统繁忙,请稍后再试");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统繁忙,请稍后再试");
        }

        return returnObject;
    }

    @RequestMapping(value = "/workbench/clue/deleteClueRemarkById.do")
    @ResponseBody
    public Object deleteClueRemarkById(String remarkId){
        ReturnObject returnObject = new ReturnObject();

        try {
            int res = clueRemarkService.deleteClueRemarkById(remarkId);
            if(res > 0){
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
            }else{
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("系统繁忙,请稍后再试");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统繁忙,请稍后再试");
        }

        return returnObject;
    }

    @RequestMapping(value = "/workbench/clue/updateClueRemarkById.do")
    @ResponseBody
    public Object updateClueRemarkById(String remarkId,String remark,HttpSession session){
        User user = (User)session.getAttribute(Contants.SESSION_USER);

        ClueRemark clueRemark = new ClueRemark();
        clueRemark.setId(remarkId);
        clueRemark.setNoteContent(remark);
        clueRemark.setEditBy(user.getId());
        clueRemark.setEditTime(DateUtils.formatDataTime(new Date()));

        ReturnObject returnObject = new ReturnObject();

        try {
            int res = clueRemarkService.updateClueRemarkById(clueRemark);
            if(res > 0){
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
                clueRemark.setEditBy(user.getName());
                returnObject.setRetData(clueRemark);
            }else{
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("系统繁忙,请稍后再试");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return  returnObject;
    }

    @RequestMapping(value = "/workbench/clue/queryActivityOfClue.do")
    @ResponseBody
    public Object queryActivityOfClue(String clueId){
        List<Activity> activityList = activityService.queryActivityOfClue(clueId);
        return activityList;
    }

    @RequestMapping(value = "/workbench/clue/queryAllActivity.do")
    @ResponseBody
    public Object queryAllActivity(){
        List<Activity> activityList = activityService.queryAllActivity();
        return activityList;
    }

    @RequestMapping(value = "/workbench/clue/bindActivityAndClue.do")
    @ResponseBody
    public Object bindActivityAndClue(String clueId,String[] activityIds){
        List<ClueActivityRelation> clueActivityRelationList = new ArrayList<>();

        for(int i = 0;i < activityIds.length;i++){
            ClueActivityRelation clueActivityRelation = new ClueActivityRelation();
            clueActivityRelation.setId(UUIDUtils.getUUID());
            clueActivityRelation.setClueId(clueId);
            clueActivityRelation.setActivityId(activityIds[i]);
            clueActivityRelationList.add(clueActivityRelation);
        }

        ReturnObject returnObject = new ReturnObject();

        try {
            int res = clueActivityRelationService.saveCreateRelations(clueActivityRelationList);
            if(res > 0){
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
            }else{
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("系统繁忙,请稍后再试");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统繁忙,请稍后再试");
        }

        return returnObject;
    }

    @RequestMapping(value = "/workbench/clue/queryAllActivityByName.do")
    @ResponseBody
    public Object queryAllActivityByName(String activityName){
        List<Activity> activityList = activityService.queryActivityByName(activityName);
        return activityList;
    }

    @RequestMapping(value = "/workbench/clue/relieveClueActivityRelation.do")
    @ResponseBody
    public Object relieveClueActivityRelation(String clueId,String activityId){
        ReturnObject returnObject = new ReturnObject();

        try {
            int res = clueActivityRelationService.deleteClueActivityRelation(clueId,activityId);
            if(res > 0){
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
            }else{
                returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("系统繁忙,请稍后再试");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Contants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("系统繁忙,请稍后再试");
        }

        return returnObject;
    }

    @RequestMapping(value = "/workbench/clue/convert.do")
    public String convert(String clueId, HttpServletRequest request){

        try {
            Clue clue = clueService.queryClueByIdForDetail(clueId);
            List<DicValue> stageList = dicValueService.queryDicValueByTypeCode("stage");

            request.setAttribute("clue",clue);
            request.setAttribute("stageList",stageList);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "workbench/clue/convert";
    }
}
